﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Project.Database.Repository
{
    public partial class DatabaseRepository
    {
        #region Properties
        #endregion

        #region Query Methods

        public AdCampain GetAdCampaign(int id)
        {
            return entities.AdCampains.SingleOrDefault(cpn => cpn.ID == id);
        }

        public AdCampain GetAdCampaignForAdvertiser(Advertiser advertiser, int campID)
        {
            advertiser.AdCampains.Load();
            return advertiser.AdCampains.SingleOrDefault(cpn => cpn.ID == campID);
        }

        public IQueryable<AdCampain> GetAdCampaignsForAdvertiser(int advID)
        {
            return from cpn in entities.AdCampains
                   where cpn.AdvertiserID == advID
                   orderby cpn.DateCreated
                   select cpn;
        }

        #endregion

        #region Insert/Delete
        public void CreateCampaign(int advId, string name, DateTime startDate, DateTime endDate, decimal startingBalance)
        {
            //TODO more checks

            if (string.IsNullOrEmpty(name.Trim()))
                throw new ArgumentException("The campaign name provided is invalid. Please check the value and try again.");
            


            Advertiser adv = GetAdvertiser(advId);
            Budget budget = adv.Budget;
            if(budget.Balance < startingBalance)
                throw new ArgumentException("Unsufficient balance.");

            budget.Balance = budget.Balance - startingBalance;

            AdCampain campaign = new AdCampain()
            {
                AdvertiserID = advId,
                Name = name,
                StartDate = startDate,
                EndDate = endDate,
                RemainingBalance = startingBalance,
                DateCreated = DateTime.Now
            };

            try
            {
                entities.AdCampains.AddObject(campaign);
                // Immediately persist the user data
                Save();
            }
            catch (ArgumentException ae)
            {
                throw ae;
            }
            catch (Exception)
            {
                throw new ArgumentException("The authentication provider returned an error. Please verify your entry and try again. " +
                    "If the problem persists, please contact your system administrator.");
            }
        }

        public void DeleteCampaign(AdCampain cpn)
        {
            if (!CampaignExists(cpn.ID))
                throw new ArgumentException("Campaign does not exist.");

            entities.AdCampains.DeleteObject(cpn);
        }

        #endregion

        #region Helper Methods

        public bool CampaignExists(int id)
        {
            return (entities.AdCampains.SingleOrDefault(c => c.ID == id) != null);
        }

        public int ReduceBalance(AdCampain campaign, decimal amount)
        {
            int val = 0;
            if (campaign.RemainingBalance - amount < 0)
                return 1;
            if (campaign.RemainingBalance - amount < 1)
                val = 2;
            campaign.RemainingBalance -= amount;
            Save();
            return val;
        }

        #endregion
    }
}